﻿using Common;
using Common.Model;
using Common.Utility;
using Common.Utility.AJ;
using Common.Utility.AJ.Extension;
using LiveChartsCore;
using LiveChartsCore.SkiaSharpView.VisualElements;
using Stylet;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Windows.Media;

namespace AWSV2.ViewModels
{
    public class WeightTotalViewModel : Screen
    {
        private IWindowManager windowManager;

        public ImageSource QRCodeImg { get; set; }

        public string QRCodeMessage { get; set; }

        public WeightStackedBarChartViewModel TotalChart { get; set; }
        public WeightStackedBarChartViewModel CustomerChart { get; set; }

        public void RefreshPage()
        {
            TotalChart.RefreshChart(TotalChart.DateRange);
            CustomerChart.RefreshChart(CustomerChart.DateRange);
        }

        public WeightTotalViewModel(IWindowManager windowManager)
        {
            this.windowManager = windowManager;
            QRCodeMessage = I18NService.Translate(LangKeys.用微信扫一扫能实时查看过磅数据);
            Prepare();
        }

        private void Prepare()
        {
            var worker = new BackgroundWorker();

            worker.DoWork += (s, e) =>
            {
                var result = new ProcessResult();

                CloudAPI.GetLocalActiveCode(SettingsHelper.AWSV2Settings, out var activeSuccess);
                if (!activeSuccess)
                {
                    result.SetError(I18NService.Translate(LangKeys.未开通));
                }
                else
                {
                    var img = Common.Utility.ImageHelper.CreateCode($"{CloudAPI.APIDOMAIN}/{CloudAPI.PID}/home/ordinary");

                    result.SetSuccess(img);
                }

                e.Result = result;
            };

            worker.RunWorkerCompleted += (s, e) =>
            {
                if (e.Error != null)
                {
                    return;
                }
                var result = e.Result as ProcessResult;
                if (!result.Success)
                {
                    QRCodeMessage = result.Message;
                    return;
                }

                QRCodeImg = result.Data as ImageSource;

                //TotalChart = new ChartData
                //{
                //    Title = new LabelVisual
                //    {
                //        Text = "总重量",
                //        Paint = new SolidColorPaint(SKColor.Parse("#333")),
                //        HorizontalAlignment = LiveChartsCore.Drawing.Align.Start,
                //    },
                //    Series = new ISeries[]
                //    {
                //        new StackedColumnSeries<int>
                //        {
                //            Values = new List<int> { 3, 5, 3, 2, 5, 4, 2 },
                //            Stroke = null,
                //            MaxBarWidth = 8,
                //            Fill = new SolidColorPaint(new SKColor(111, 209, 246))

                //        },
                //        new StackedColumnSeries<int>
                //        {
                //            Values = new List<int> { 4, 2, 3, 2, 3, 4, 2 },
                //            Stroke = null,
                //            DataLabelsPaint =null,
                //            MaxBarWidth = 8,
                //            Fill = new SolidColorPaint(new SKColor(107, 115, 202))
                //        },
                //    }
                //};

                //CustomerChart = new ChartData
                //{
                //    Title = new LabelVisual
                //    {
                //        Text = "客户称重数据",
                //        Paint = new SolidColorPaint(SKColor.Parse("#333")),
                //        HorizontalAlignment = LiveChartsCore.Drawing.Align.Start
                //    },
                //    Series = TotalChart.Series
                //};
            };

            worker.RunWorkerAsync();

            TotalChart = new WeightStackedBarChartViewModel
            {
                Title = I18NService.Translate(LangKeys.总重量),
                DateRange = DateRangeType.FifteenDays,
                LabelFormatHandle = (value) =>
                {
                    return ((DateTime)value).ToString("MM-dd");
                }
            };

            TotalChart.RefreshChart(TotalChart.DateRange);

            CustomerChart = new WeightStackedBarChartViewModel
            {
                Title = I18NService.Translate(LangKeys.客户称重数据),
                DateRange = DateRangeType.SevenDays,
                AvailableDateRanges = new DateRangeType[] {
                    DateRangeType.SevenDays,
                    DateRangeType.OneMonth ,
                    DateRangeType.ThreeMonth ,
                },
                XAxesHandle = (source, _, __) =>
                 {
                     var keys = source.GroupBy(p => p.Kh).Select(p => p.Key).ToList();
                     var labels = new List<object>();
                     for (int i = 0; i < keys.Count; i++)
                     {
                         labels.Add(keys.ElementAtOrDefault(i));
                     }
                     return labels;
                 },
                JZSumHandle = (source, key) =>
                {
                    var value = key.ToString();
                    return source
                        .Where(p => p.Kh == value)
                        .Sum(p => p.Jz);
                },
                PZSumHandle = (source, key) =>
                {
                    var value = key.ToString();
                    return source
                        .Where(p => p.Kh == value)
                        .Sum(p => p.Pz);
                },
                MZSumHandle = (source, key) =>
                 {
                     var value = key.ToString();
                     return source
                         .Where(p => p.Kh == value)
                         .Sum(p => p.Mz);
                 },
                LabelFormatHandle = (value) =>
                {
                    return value?.ToString().Overflow(5);
                }
            };

            CustomerChart.RefreshChart(CustomerChart.DateRange);

        }


    }

    public class ChartData : PropertyChangedBase
    {
        private LabelVisual _title;
        public LabelVisual Title
        {
            get
            {
                return _title;
            }
            set
            {
                SetAndNotify(ref _title, value);
            }
        }

        private ISeries[] _series;
        public ISeries[] Series
        {
            get
            {
                return _series;
            }
            set
            {
                SetAndNotify(ref _series, value);
            }
        }

    }
}